<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="唯一标识"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="输入框名称"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="输入框显示值"%>
<%@ attribute name="userId" type="java.lang.String" required="true" description="用户Id"%>
<%@ attribute name="checks" type="java.util.List" required="true" description="所选内容Id"%>
<%@ attribute name="checkedList" type="java.util.List" required="true" description="所选内容List"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="相应Url"%>

	<input id="${id}StoreLabel" value="${not empty value?value:''}"  maxlength="100" readonly="true" style="border-radius: 3px;"/>&nbsp;
	<input id="url" value="${url}" type="hidden">
	<input id="checkedList" value="${checkedList}" type="hidden">
	<input id="${id}checks" name="${name}" type="hidden" value="${checks}"/>
	<a id="${id}Button" href="javascript:" class="btn">选择</a>&nbsp;&nbsp;
<script type="text/javascript">


	$("#${id}Button").click(function(){
		top.$.jBox.open("iframe:"+$("#url").val(), "选择门店", 700, $(top.document).height()-180, {
            buttons:{"确定":"ok", "清除":"clear", "关闭":true}, submit:function(v, h, f){
                if (v=="ok"){
                	var result="";
                	var contentWindow=h.find("iframe")[0].contentWindow;
                	var len= contentWindow.$("input[name='storeList']").length;
                	var checks=contentWindow.$("input[name='storeList']");
                	var labels=contentWindow.$("label");
                	var idArray = new Array();
                	for(var i=0;i<len;i++){
                		if(checks[i].checked){
                			result=result+labels[i].innerHTML+",";
                			idArray.push(checks[i].value);
                		}
                	}
                	if(result!=""){
                		result=result.substring(0,result.length-1);
                		$("#${id}StoreLabel").val(result);
                		$("#${id}checks").val(idArray);
                	}else{
                		$("#${id}StoreLabel").val("");
                		$("#${id}checks").val("");
                	}
	                
                }else if (v=="clear"){
                	var contentWindow=h.find("iframe")[0].contentWindow;
                	var checks=contentWindow.$('input');
                	for(var i=0;i<len;i++){
                		checks[i].checked=false;
                	}
	                $("#${id}StoreLabel").val("");
	                $("#${id}checks").val("");
                }
            }, loaded:function(h){
                $(".jbox-content", top.document).css("overflow-y","hidden");
                var checks=h.find("iframe")[0].contentWindow.$("input[name='storeList']");//全部数据
                for(var i=0,length=checks.length;i<length;i++){//全部数据初始化
                	checks[i].checked=false;
                }
                var curCheck=$("#${id}checks").val();
                if(curCheck.indexOf("[")==-1){
                	curCheck="["+curCheck+"]";
                }
                var cks=eval("("+curCheck+")");//form页选中数据
                for(var i=0,length=checks.length;i<length;i++){
                	var boo=false;
                	var id=checks[i].value;
                	if(cks!=null&&cks.length>0){
                		for(var j=0,len=cks.length;j<len;j++){
                        	var ckId=cks[j];
                        	if(ckId==id){
                        		boo=true;
                        	}
                        }
                	}
                	if(boo){
                		checks[i].checked=true;
                	}
                }
                //全选
                var areaChecks=h.find("iframe")[0].contentWindow.$("input[name='areaStore']");
                for(var i=0,len=areaChecks.length;i<len;i++){
                	var flag=0;
                	var areaIds=eval("("+areaChecks[i].value+")");
                	if(cks!=null&&cks.length>0){
                		for(var j=0,length=cks.length;j<length;j++){
                			var checkId=cks[j];
                			if(areaIds.indexOf(checkId)>-1){
                				flag=flag+1;
                			}
                		}
                	}
                	if(flag==Number(areaIds.length)){
                		areaChecks[i].checked=true;
                	}
                }
            }
        });
	});
	
</script>
